/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.domain.operator;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.business.customer.facade.enums.operator.CustomerOperatorRole;
import info.peigen.hotpot.business.customer.facade.enums.operator.CustomerOperatorStatus;
import info.peigen.hotpot.common.core.domain.DomainObject;
import info.peigen.hotpot.common.core.enums.YesOrNo;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;
import lombok.experimental.SuperBuilder;

import java.util.List;

import static info.peigen.hotpot.business.customer.facade.enums.operator.CustomerOperatorRole.Master;
import static info.peigen.hotpot.business.customer.facade.enums.operator.CustomerOperatorStatus.Init;
import static info.peigen.hotpot.common.core.enums.YesOrNo.N;

/**
 * <b>(CustomerOperator)</b>
 * 操作员领域对象
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/7/28
 */
@EqualsAndHashCode(callSuper = true)
@SuperBuilder
@Data
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Accessors(chain = true)
public class CustomerOperator extends DomainObject {
    /** 默认只有一个Master操作员的时候,改Id与customerId一样 */
    String operatorId;
    /** 登录名-可用手机号或邮箱 */
    String loginName;
    /** 登录密码 */
    String loginPassword;
    /** 密码盐 */
    String salt;
    /** 交易密码 */
    String payPassword;
    /** 状态 */
    @Builder.Default
    CustomerOperatorStatus status               = Init;
    /** 是否主操作员 */
    @Builder.Default
    CustomerOperatorRole   customerOperatorRole = Master;
    /** 是否删除标记 */
    @Builder.Default
    YesOrNo                deleteFlag           = N;
    /** 活动记录 */
    CustomerOperatorActive activeInfo;
    /** 基础信息 */
    CustomerOperatorBase   baseInfo;

    public static CustomerOperator findMasterOperator(List<CustomerOperator> operators) {
        return operators.stream().filter(operator -> operator.getCustomerOperatorRole() == Master).findFirst().orElse(null);
    }

    @Override
    public void check() {
        if (status != Init) {
            Assert.isTrue(StrUtil.isAllNotEmpty(loginName, loginPassword));
        }
    }
}
